package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.LossesLog;
import com.sjlh.centurylianhua.mapper.LosseslogMapper;
import com.sjlh.centurylianhua.mapper.UserMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/losseslog")
@CrossOrigin
public class LosseslogController {

    @Autowired
    private LosseslogMapper losses;

    @RequestMapping("/all")
    public ResultData getAll(@RequestBody SearchCon sc){
//      分页对象，传入当前页码及每页的数量
        QueryWrapper<LossesLog> wra=new QueryWrapper<LossesLog>();
        wra.like(StringUtils.isnotNull(sc.getName()),"user.userName",sc.getName());
        wra.eq(sc.getTypeName2()!=null,"lo.depotId",sc.getTypeName2());
        wra.eq(sc.getTypeName3()!=null,"lo.productId",sc.getTypeName3());
        wra.ge(StringUtils.isnotNull(sc.getBeginDate()),"lo.createTime",sc.getBeginDate());
        wra.le(StringUtils.isnotNull(sc.getEndDate()),"lo.createTime",sc.getEndDate());
        wra.apply("user.id = lo.userId and product.id=lo.productId and depot.id=lo.depotId");
        //封装分页对象
        if(sc.getPage()!=null) {
            Page<LossesLog> pa=new Page<LossesLog>(sc.getPage(),sc.getLimit());
            List<LossesLog> uspa=losses.getInfo(pa,wra);
            return new ResultData(true,20000," " ,uspa,pa.getTotal());
        }else{
            Long count= Long.valueOf(losses.selectCount(wra));
            return new ResultData(true,20000," " ,losses.getInfo(wra),count);
        }
    }
    @RequestMapping("/add")
    public ResultData add(@RequestBody LossesLog loss){
        if(loss!=null&& !"".equals(loss) &&loss.getUserId()!=null){
                    System.out.println(loss);
                int r=losses.insert(loss);
                    return new ResultData(true, 20000, "数据提交成功!", true, null);
        }else{
            return new ResultData(false,20000,"数据不能为空!" ,false,null);
        }
    }
}
